import sys
import json
from PyQt5 import QtCore, QtGui, QtWidgets
from file_config import pri_key_or_psd
import file_config 
import importlib
from tools.aes_enc import encrypt_message_dict
from file_tools import save_decrypted_data
class MainIndexWindows(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.PlainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
        self.PlainTextEdit.setGeometry(QtCore.QRect(0, 0, 801, 431))
        self.PlainTextEdit.setObjectName("PlainTextEdit")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(40, 440, 131, 61))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(250, 440, 131, 61))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(440, 440, 131, 61))
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(630, 440, 131, 61))
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_5.setGeometry(QtCore.QRect(70, 520, 661, 41))
        self.pushButton_5.setObjectName("pushButton_5")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.pushButton.clicked.connect(self.show_bank_card_dialog)
        self.pushButton_2.clicked.connect(self.show_email_dialog)
        self.pushButton_3.clicked.connect(self.show_personal_info_dialog)
        self.pushButton_4.clicked.connect(self.show_website_info_dialog)
        self.pushButton_5.clicked.connect(self.show_data_query_dialog)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "增加银行卡信息"))
        self.pushButton_2.setText(_translate("MainWindow", "增加邮箱信息"))
        self.pushButton_3.setText(_translate("MainWindow", "增加个人信息"))
        self.pushButton_4.setText(_translate("MainWindow", "增加网址信息"))
        self.pushButton_5.setText(_translate("MainWindow", "信息查询"))

    def show_bank_card_dialog(self):
        dialog = QtWidgets.QDialog()
        dialog.setWindowTitle("增加银行卡信息")

        layout = QtWidgets.QVBoxLayout()

        layout.addWidget(QtWidgets.QLabel("银行卡账号:"))
        bank_card_number = QtWidgets.QLineEdit()
        layout.addWidget(bank_card_number)

        layout.addWidget(QtWidgets.QLabel("银行卡密码:"))
        bank_card_password = QtWidgets.QLineEdit()
        layout.addWidget(bank_card_password)

        layout.addWidget(QtWidgets.QLabel("用户名字:"))
        user_name = QtWidgets.QLineEdit()
        layout.addWidget(user_name)

        layout.addWidget(QtWidgets.QLabel("用户身份证号码:"))
        user_id_number = QtWidgets.QLineEdit()
        layout.addWidget(user_id_number)

        save_button = QtWidgets.QPushButton("保存")
        save_button.clicked.connect(lambda: self.save_data_to_json(dialog, {
            "type":"aes",
            "file_type": "bank_card",
            "bank_card_number": bank_card_number.text(),
            "bank_card_password": bank_card_password.text(),
            "user_name": user_name.text(),
            "user_id_number": user_id_number.text()
        }))
        layout.addWidget(save_button)

        dialog.setLayout(layout)
        dialog.exec_()

    def show_email_dialog(self):
        dialog = QtWidgets.QDialog()
        dialog.setWindowTitle("增加邮箱信息")

        layout = QtWidgets.QVBoxLayout()

        layout.addWidget(QtWidgets.QLabel("邮箱账号:"))
        email_account = QtWidgets.QLineEdit()
        layout.addWidget(email_account)

        layout.addWidget(QtWidgets.QLabel("邮箱密码:"))
        email_password = QtWidgets.QLineEdit()
        layout.addWidget(email_password)

        layout.addWidget(QtWidgets.QLabel("邮箱所登录的网站:"))
        email_website = QtWidgets.QLineEdit()
        layout.addWidget(email_website)

        layout.addWidget(QtWidgets.QLabel("邮箱的申请者:"))
        email_applicant = QtWidgets.QLineEdit()
        layout.addWidget(email_applicant)

        save_button = QtWidgets.QPushButton("保存")
        save_button.clicked.connect(lambda: self.save_data_to_json(dialog, {
            "type":"aes",
            "file_type": "email",
            "email_account": email_account.text(),
            "email_password": email_password.text(),
            "email_website": email_website.text(),
            "email_applicant": email_applicant.text()
        }))
        layout.addWidget(save_button)

        dialog.setLayout(layout)
        dialog.exec_()

    def show_personal_info_dialog(self):
        dialog = QtWidgets.QDialog()
        dialog.setWindowTitle("增加个人信息")

        layout = QtWidgets.QVBoxLayout()

        layout.addWidget(QtWidgets.QLabel("用户名字:"))
        user_name = QtWidgets.QLineEdit()
        layout.addWidget(user_name)

        layout.addWidget(QtWidgets.QLabel("用户的身份证号码:"))
        user_id_number = QtWidgets.QLineEdit()
        layout.addWidget(user_id_number)

        layout.addWidget(QtWidgets.QLabel("用户的家庭住址:"))
        user_address = QtWidgets.QLineEdit()
        layout.addWidget(user_address)

        layout.addWidget(QtWidgets.QLabel("用户的手机号:"))
        user_phone = QtWidgets.QLineEdit()
        layout.addWidget(user_phone)

        layout.addWidget(QtWidgets.QLabel("用户的邮箱:"))
        user_email = QtWidgets.QLineEdit()
        layout.addWidget(user_email)

        save_button = QtWidgets.QPushButton("保存")
        save_button.clicked.connect(lambda: self.save_data_to_json(dialog, {
            "type":"rsa",
            "file_type": "personal_info",
            "user_name": user_name.text(),
            "user_id_number": user_id_number.text(),
            "user_address": user_address.text(),
            "user_phone": user_phone.text(),
            "user_email": user_email.text()
        }))
        layout.addWidget(save_button)

        dialog.setLayout(layout)
        dialog.exec_()

    def show_website_info_dialog(self):
        dialog = QtWidgets.QDialog()
        dialog.setWindowTitle("增加网址信息")

        layout = QtWidgets.QVBoxLayout()

        layout.addWidget(QtWidgets.QLabel("网站的URL:"))
        website_url = QtWidgets.QLineEdit()
        layout.addWidget(website_url)

        layout.addWidget(QtWidgets.QLabel("网站的关键词:"))
        website_keywords = QtWidgets.QLineEdit()
        layout.addWidget(website_keywords)

        layout.addWidget(QtWidgets.QLabel("网站的账号:"))
        website_account = QtWidgets.QLineEdit()
        layout.addWidget(website_account)

        layout.addWidget(QtWidgets.QLabel("网站的密码:"))
        website_password = QtWidgets.QLineEdit()
        layout.addWidget(website_password)

        save_button = QtWidgets.QPushButton("保存")
        save_button.clicked.connect(lambda: self.save_data_to_json(dialog, {
            "type":"rsa",
            "file_type": "website",
            "website_url": website_url.text(),
            "website_keywords": website_keywords.text(),
            "website_account": website_account.text(),
            "website_password": website_password.text()
        }))
        layout.addWidget(save_button)

        dialog.setLayout(layout)
        dialog.exec_()

    def show_data_query_dialog(self):
        dialog = QtWidgets.QDialog()
        dialog.setWindowTitle("信息查询")

        layout = QtWidgets.QVBoxLayout()

        button_bank_card = QtWidgets.QPushButton("银行卡信息")
        button_bank_card.clicked.connect(lambda: self.show_data_in_line_edit(dialog, "bank_card"))
        layout.addWidget(button_bank_card)

        button_email = QtWidgets.QPushButton("邮箱信息")
        button_email.clicked.connect(lambda: self.show_data_in_line_edit(dialog, "email"))
        layout.addWidget(button_email)

        button_personal_info = QtWidgets.QPushButton("个人信息")
        button_personal_info.clicked.connect(lambda: self.show_data_in_line_edit(dialog, "personal_info"))
        layout.addWidget(button_personal_info)

        button_website = QtWidgets.QPushButton("网址信息")
        button_website.clicked.connect(lambda: self.show_data_in_line_edit(dialog, "website"))
        layout.addWidget(button_website)

        dialog.setLayout(layout)
        dialog.exec_()

    def save_data_to_json(self, dialog, data):
        importlib.reload(file_config)
        pri_key_or_psd = file_config.pri_key_or_psd
        encrypted_message, encrypt_type, pri_key_or_psd = encrypt_message_dict(data, pri_key_or_psd,user_nums=1)
        save_decrypted_data(1)

    def show_data_in_line_edit(self, dialog, data_type=None):
        from file_config import last_user
        try:
            data_list = []
            with open(f'C:\\Users\\29741\\Desktop\\test\\file\\user{last_user}\\code{last_user}.py', 'r', encoding='utf-8') as file:
                for line in file:
                    try:
                        data = eval(line.strip())  # 使用 eval 将每行字符串转化为字典
                        data_list.append(data)
                    except (SyntaxError, NameError):
                        self.PlainTextEdit.setPlainText("数据文件格式错误。")
                        return

            if data_type:
                filtered_data = [data for data in data_list if data.get("file_type") == data_type]
            else:
                filtered_data = data_list

            display_text = '\n'.join(json.dumps(data, ensure_ascii=False) for data in filtered_data)

            # 设置字体大小为 10
            font = self.PlainTextEdit.font()
            font.setPointSize(10)
            self.PlainTextEdit.setFont(font)

            self.PlainTextEdit.setPlainText(display_text)


        except FileNotFoundError:
            self.PlainTextEdit.setPlainText("数据文件未找到。")
        except json.JSONDecodeError:
            self.PlainTextEdit.setPlainText("数据文件格式错误。")
        dialog.accept()



# if __name__ == "__main__":

#     app = QtWidgets.QApplication(sys.argv)
#     MainWindow = QtWidgets.QMainWindow()
#     ui = MainIndexWindows()
#     ui.show_data_in_line_edit
#     ui.setupUi(MainWindow)
#     MainWindow.show()
#     sys.exit(app.exec_())
